// 070731;080416 alexch
//
// ==========  This file is under  LGPL, the GNU Lesser General Public Licence
// ==========  Dialing Lemmatizer (www.aot.ru)
// ==========  Copyright by Alexey Sokirko 
//
// ==========  Visual Studio project for 32/64 bit DLL-wrapper
// ==========  Created by Hulbee AG

#ifndef __STLWINF_H_
#define __STLWINF_H_

#include "StlSysF.h"
#include <windows.h>

namespace StlFunc {

bool StrA2W(const str_t &src, wstr_t &dst);
bool StrA2W(const char *src, wstr_t &dst);
bool StrW2A(const wstr_t &src, str_t &dst, LPCSTR DefaultChar = 0, LPBOOL UsedDefaultChar = 0);
bool ExactStrW2A(const wstr_t &src, str_t &dst);
bool StrW2A(const wchar_t *src, str_t &dst, LPCSTR DefaultChar = 0, LPBOOL UsedDefaultChar = 0);
bool ExactStrW2A(const wchar_t *src, str_t &dst);

str_t& UpperCase(str_t &s);
wstr_t& UpperCase(wstr_t &s);

str_t& LowerCase(str_t &s);
wstr_t& LowerCase(wstr_t &s);

int StrCmp(const str_t &str1, const str_t &str2, unsigned int maxCmpChars = 0);
int StrICmp(const str_t &str1, const str_t &str2, unsigned int maxCmpChars = 0);
int WStrCmp(const wstr_t &str1, const wstr_t &str2, unsigned int maxCmpChars = 0);
int WStrICmp(const wstr_t &str1, const wstr_t &str2, unsigned int maxCmpChars = 0);

bool ExpandEnvVars(str_t &s);
bool ExpandEnvVars(wstr_t &s);

bool GetSystemName(str_t &s);
bool GetSystemName(wstr_t &s);

#if (_WIN32_WINNT >= 0x0500)
	bool GetSystemNameEx(str_t &s, COMPUTER_NAME_FORMAT fmt = ComputerNameNetBIOS);
	bool GetSystemNameEx(wstr_t &s, COMPUTER_NAME_FORMAT fmt = ComputerNameNetBIOS);
#endif

// system errors

bool SysErrorMessage(DWORD code, wstr_t &dst);
wstr_t SysErrorMessage(DWORD code);
// return a Win32 error description (removing trailing spaces and dots)

// date & time

bool FileTimeToString(FILETIME& fileTime, wstr_t &strDateTime, bool date = true, bool time = true);

// privileges

enum AdjustPrivilegesError {
	ape_None, ape_OpenProcessToken, ape_LookupPrivilegeValue,
	ape_AdjustTokenPrivileges
};

bool AdjustPrivileges(const wchar_t *names[], DWORD name_count, AdjustPrivilegesError* code = 0, DWORD* lastError = 0);

// Console input/output

class Con {
protected:
	HANDLE m_hStdOut, m_hStdIn;
	bool m_redirOut, m_redirIn;
public:
	Con();
	void Reinit(bool writePrefix);
	// writePrefix == true: write UTF-8 "prefix" (only if RedirOut())
	bool InOK()
	{
		return m_hStdIn != INVALID_HANDLE_VALUE && m_hStdIn != 0;
	}
	bool OutOK()
	{
		return m_hStdOut != INVALID_HANDLE_VALUE && m_hStdOut != 0;
	}
	bool Print(const wchar_t *buf, DWORD len);
	// if stdout is redirected then writes in UTF-8
	bool Print(const wchar_t *buf)
	{
		return Print(buf, (DWORD)wcslen(buf));
	}
	bool Printf(const wchar_t *fmt, ...);
	bool Print(const wstr_t &w)
	{
		return Print(w.data(), (DWORD)w.size());
	}
	bool Print(const char *buf, DWORD len);
	bool Print(const char *buf)
	{
		return Print(buf, (DWORD)strlen(buf));
	}
	bool Printf(const char *fmt, ...);
	bool Print(const str_t &s)
	{
		return Print(s.data(), (DWORD)s.size());
	}
	bool RedirIn()
	{
		return m_redirIn;
	}
	bool RedirOut()
	{
		return m_redirOut;
	}
	bool RedirInOrOut()
	{
		return m_redirIn || m_redirOut;
	}
	HANDLE StdOut()
	{
		return m_hStdOut;
	}
	HANDLE StdIn()
	{
		return m_hStdIn;
	}
};

}

#endif
